package bean;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

public class AccountDAO {

    DataSource ds;

    public AccountDAO() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/xmart?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");
        config.setDriverClassName("com.mysql.jdbc.Driver");
        config.setUsername("root");
        config.setPassword("123456");
        config.addDataSourceProperty("connectionTimeout", "1000"); // 连接超时：1秒
        config.addDataSourceProperty("idleTimeout", "60000"); // 空闲超时：60秒
        config.addDataSourceProperty("maximumPoolSize", "800"); // 最大连接数：10
        ds = new HikariDataSource(config);

    }

    public int add(Account account) {
        String sql = "insert into account values (null,?,?,?,?,?,?,?)";
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ps.setString(1, account.getAccountName());
            ps.setString(2, account.getPassword());
            ps.setInt(3, 0);
            ps.setInt(4, 0);
            ps.setInt(5, 0);
            ps.setString(6, "");
            ps.setString(7, "");
            ps.execute();
            ResultSet rs = ps.executeQuery("SELECT LAST_INSERT_ID()");
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
        return -1;
    }


    public void delete(int id) {
        String sql = "delete from account where id = " + id;
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ps.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//
//    public static void main(String[] args) {
//        AccountDAO as =new AccountDAO();
//        Account account = as.get("3@qq.com");
//        System.out.println(account.getPassword());
//
//    }
    public Account get(String accountName) {
        Account account = null;
        String sql = "select * from account where name = '" +accountName+ "' limit 1";
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                account = new Account();
                account.setAccountID(rs.getInt("id"));
                account.setAccountName(rs.getString("name"));
                account.setPassword(rs.getString("password"));
                account.setLevel(rs.getInt("level"));
                account.setCoins(rs.getInt("coin"));
                account.setPoints(rs.getInt("point"));
                if(rs.getString("questionList")!=null){
                    ArrayList<String> arrayList = new ArrayList<>();
                    String[] s = rs.getString("questionList").split(",");
                    arrayList.addAll(Arrays.asList(s));
                    account.setQuestionList(arrayList);
                }
                if(rs.getString("answerList")!=null){
                    ArrayList<String> arrayList = new ArrayList<>();
                    String[] s = rs.getString("answerList").split(",");
                    arrayList.addAll(Arrays.asList(s));
                    account.setAnswerList(arrayList);
                }

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return account;
    }

    public Account get(int id) {
        Account account = null;
        String sql = "select * from account where id = " + id;
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                account = new Account();
                account.setAccountID(rs.getInt("id"));
                account.setAccountName(rs.getString("name"));
                account.setPassword(rs.getString("password"));
                account.setLevel(rs.getInt("level"));
                account.setCoins(rs.getInt("coin"));
                account.setPoints(rs.getInt("point"));
                if(rs.getString("answerList")!=null){
                    ArrayList<String> arrayList = new ArrayList<>();
                    String[] s = rs.getString("answerList").split(",");
                    arrayList.addAll(Arrays.asList(s));
                    account.setAnswerList(arrayList);
                }
                if(rs.getString("questionList")!=null){
                    ArrayList<String> arrayList = new ArrayList<>();
                    String[] s = rs.getString("answerList").split(",");
                    arrayList.addAll(Arrays.asList(s));
                    account.setQuestionList(arrayList);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return account;
    }


    public void update(Account account) {

        String sql = "update account set name = ?, point = ?, level = ? , coin = ? , questionList = ?, answerList = ?where id = ?";
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ps.setString(1, account.getAccountName());
            ps.setInt(2, account.getPoints());
            ps.setInt(3, account.getLevel());
            ps.setInt(4, account.getCoins());
            ps.setString(5, String.join(",", account.questionList));
            ps.setString(6, String.join(",", account.answerList));
            ps.setInt(7, account.getAccountID());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
